"""
Case Type   : cm
Case Name   : cm_ctl switchover操作未指定-D参数
Create At   : 2024-07
Owner       : haomeng
Description :
    1、查看集群状态
    2、执行cm_ctl switchover -n
    3、执行cm_ctl switchover -n nodeid
Expect      :
    1、集群所有节点状态正常，显示Normal
    2、合理报错
    3、合理报错
History     :
"""

import os
import re
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class CmCtl(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_cm(self):
        text = '----step1:查看集群状态; expect:集群状态正常,DN主在节点1----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn("cluster_state   : Normal", status)

        text = '----step2:执行cm_ctl switchover -n; expect:合理报错----'
        self.log.info(text)

        switch1 = self.comsh.exec_cm_ctl('switchover', '-n')
        self.log.info(switch1)
        self.assertIn('option requires an argument', switch1, '切换成功')

        text = '------step3:执行cm_ctl switchover -n nodeid; expect:合理报错------'
        self.log.info(text)

        switch2 = self.comsh.exec_cm_ctl('switchover', '-n 2')
        self.log.info(switch2)
        self.assertIn('no data directory specified', switch2, '切换成功')

    def tearDown(self):
        self.log.info('-----No need to clean-----')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
